package com.yuan.zsschool.ui.notice;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yuan.base.framework.BaseFrameworkActivity;
import com.yuan.base.framework.BaseViewHolderAdapter;
import com.yuan.base.framework.image.BaseImageLoad;
import com.yuan.base.utils.DateUtil;
import com.yuan.base.widget.AutoLoadList;
import com.yuan.zsschool.R;
import com.yuan.zsschool.bean.Notice;
import com.yuan.zsschool.common.CacheManager;

import java.util.List;

/**
 * 通告
 */
public class NoticeActivity extends BaseFrameworkActivity {

	// --------------------------控件--------------------------
	private AutoLoadList auto_load_list;
	private ListView data_list;// 下拉刷新
	// --------------------------变量--------------------------
	private List<Notice> dataList;
	private NoticeAdapter noticeAdapter;
	private int index = 1;
	private int type = 0;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_notice;
	}

	@Override
	protected void bindViews() {
		auto_load_list = findViewById(R.id.auto_load_list);
		data_list = auto_load_list.getData_list();
		auto_load_list.setOnRefreshingListener(new AutoLoadList.OnRefreshingListener() {
			@Override
			public void onRefresh() {
				getListData(AutoLoadList.OPT_REFREASH);
			}

			@Override
			public void onLoadMore() {
				getListData(AutoLoadList.OPT_LOADMORE);
			}
		});

		data_list.setOnItemClickListener((parent, view, position, id) -> {
			Intent intent = new Intent(this, NoticeDetailActivity.class);
			intent.putExtra("notice", (Notice) parent.getItemAtPosition(position));
			startActivity(intent);
		});
		setOnClickListener(R.id.fl_add, v -> {
			Intent intent = new Intent(this, NoticeAddActivity.class);
			intent.putExtra("type", type);
			startActivity(intent);
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		type = getIntent().getIntExtra("type", -1);
		if (type == -1) {
			showToast("发生错误");
			closeSelf();
			return;
		}
		auto_load_list.startRefreshing();
		CacheManager.getInstance().initNotificationData(new CacheManager.HandleCallback<List<Notice>>() {
			@Override
			public void onCacheSuccess(List<Notice> result) {
			}

			@Override
			public void onHttpSuccess(List<Notice> result) {
				// 从缓存取第一条数据比对，判断是否有更新
				List<Notice> temp = CacheManager.getInstance().getNoticeList(type, 1, 1);
				if (temp != null && temp.size() > 0) {
					// 表示数据有更新，刷新列表
					if (dataList == null || dataList.isEmpty()) {
						auto_load_list.startRefreshing();
					} else if (dataList.get(0).getId() != result.get(0).getId()) {
						auto_load_list.startRefreshing();
					}
				}
			}

			@Override
			public void onError() {
			}
		});
	}

	private void getListData(final int opt) {
		if (opt == AutoLoadList.OPT_INIT || opt == AutoLoadList.OPT_REFREASH) {
			index = 1;
		}
		if (opt == AutoLoadList.OPT_LOADMORE) {
			index++;
		}

		List<Notice> temp = CacheManager.getInstance().getNoticeList(type, index, auto_load_list.getRowCount());
		switch (opt) {
			case AutoLoadList.OPT_INIT:
			case AutoLoadList.OPT_REFREASH:
				dataList = temp;
				init(true);
				break;
			case AutoLoadList.OPT_LOADMORE:
				dataList.addAll(temp);
				init(false);
				break;
		}
		auto_load_list.completeRefresh();
	}

	private void init(boolean needNew) {
		if (needNew) {
			noticeAdapter = new NoticeAdapter(this);
			noticeAdapter.setData(dataList);
			data_list.setAdapter(noticeAdapter);
		} else {
			noticeAdapter.setData(dataList);
			noticeAdapter.notifyDataSetChanged();
		}
	}

	private class NoticeAdapter extends BaseViewHolderAdapter<Notice, Holder> {

		private NoticeAdapter(Context context) {
			super(context);
		}

		@Override
		protected void bindViews(View convertView, Holder holder, Notice item) {
			holder.tv_notice_title = convertView.findViewById(R.id.tv_notice_title);
			holder.tv_notice_publisher = convertView.findViewById(R.id.tv_notice_publisher);
			holder.tv_notice_publish_time = convertView.findViewById(R.id.tv_notice_publish_time);
			holder.iv_notice_first_img = convertView.findViewById(R.id.iv_notice_first_img);
		}

		@Override
		protected View getConvertView(Notice item, LayoutInflater inflater, int position, ViewGroup parent) {
			return inflater.inflate(R.layout.item_notice_list, parent, false);
		}

		@Override
		protected Holder getHolder() {
			return new Holder();
		}

		@Override
		protected void refreshView(int position, Notice item, View convertView, Holder holder) {
			holder.tv_notice_title.setText(item.getTitle());
			holder.tv_notice_publisher.setText(item.getPublisher());
			holder.tv_notice_publish_time.setText(DateUtil.dateToString(item.getPublishDate()).substring(5, 16));
			if (item.getPicsList() != null && item.getPicsList().length > 0) {
				BaseImageLoad.load(item.getPicsList()[0], holder.iv_notice_first_img);
			} else {
				holder.iv_notice_first_img.setImageResource(R.drawable.base_ic_loading);
			}
		}
	}

	private class Holder {
		TextView tv_notice_title;
		TextView tv_notice_publisher;
		TextView tv_notice_publish_time;
		ImageView iv_notice_first_img;
	}
}

